package com.wgchao.identityrightsmanagement.infrastructure.dao;

import com.wgchao.identityrightsmanagement.domain.resource.model.ProjectResources;
import com.wgchao.identityrightsmanagement.domain.resource.repository.ProjectRepository;
import com.wgchao.identityrightsmanagement.infrastructure.convert.ProjectConvert;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectExistException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceProject;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ProjectDao
 * @author: wgchao
 * @createTime: 2020/6/18 9:45 AM
 * @Description: TODO
 */
@Component
public class ProjectDao implements ProjectRepository {
    @Autowired
    private SysResourceProjectRepository sysResourceProjectRepository;
    @Autowired
    private ProjectConvert projectConvert;

    @Override
    public ProjectResources findById(String s) {
        SysResourceProject sysResourceProject = sysResourceProjectRepository.findFirstByProMark(s);
        if(sysResourceProject == null){
            return null;
        }
        return projectConvert.convert(sysResourceProject);
    }

    @Override
    public void remove(ProjectResources projectResources, String operator) {
        SysResourceProject sysResourceProject = sysResourceProjectRepository.findFirstByProMark(projectResources.getProMark());
        if(sysResourceProject == null){
            throw new ObjectNotExistException("项目不存在");
        }
        sysResourceProjectRepository.delete(sysResourceProject);
        sysResourceProjectRepository.findFirstByProMark(projectResources.getProMark());
    }

    @Override
    public void add(ProjectResources projectResources, String operator) {
        if(projectResources.getId().compareTo(0L) < 0){
            SysResourceProject sysResourceProject = sysResourceProjectRepository.findFirstByProMark(projectResources.getProMark());
            if(sysResourceProject != null){
                throw new ObjectExistException("项目标识已被占用");
            }
            sysResourceProject = projectConvert.convert(projectResources);
            sysResourceProject.setId(null);
            sysResourceProject.setCreateTime(new Date());
            sysResourceProject.setCreator(operator);
            sysResourceProject = sysResourceProjectRepository.save(sysResourceProject);
        }else{
            SysResourceProject sysResourceProject_old = sysResourceProjectRepository.findFirstByProMark(projectResources.getProMark());
            if(sysResourceProject_old == null){
                throw new ObjectNotExistException("项目不存在");
            }
            SysResourceProject sysResourceProject = projectConvert.convert(projectResources);
            sysResourceProject.setLastUpdator(operator);
            sysResourceProject.setLastUpdateTime(new Date());
            sysResourceProject.setCreator(sysResourceProject_old.getCreator());
            sysResourceProject.setCreateTime(sysResourceProject_old.getCreateTime());
            sysResourceProject = sysResourceProjectRepository.save(sysResourceProject);
        }
    }

    @Override
    public void addAll(List<ProjectResources> projectResources, String operator) {
        for(ProjectResources projectResources1 : projectResources){
            add(projectResources1, operator);
        }
    }

    @Override
    public void removeAll(List<ProjectResources> projectResources, String operator) {
        for(ProjectResources projectResources1 : projectResources){
            remove(projectResources1, operator);
        }
    }
}
